<?php

/**
 * Author: xx.com
 * Date: 2015-09-09
 */

namespace Admin\Controller;
use Think\Controller;
use Admin\Logic\UpgradeLogic;
class BaseController extends Controller {

    // 当前管理员的信息
    protected $admin_info = NULL;

    // 当前登录的店铺信息
    protected $shop_info = NULL;

    /**
     * 析构函数
     */
    function __construct() 
    {
        parent::__construct();
        $upgradeLogic = new UpgradeLogic();
        $upgradeMsg = $upgradeLogic->checkVersion(); //升级包消息        
        $this->assign('upgradeMsg',$upgradeMsg);    
        //用户中心面包屑导航
        $navigate_admin = navigate_admin();
        $this->assign('navigate_admin',$navigate_admin);
        tpversion();
   }    
    
    /*
     * 初始化操作
     */
    public function _initialize() 
    {
        $this->assign('action',ACTION_NAME);
        //过滤不需要登陆的行为
        if(in_array(ACTION_NAME,array('login','logout','vertify')) || in_array(CONTROLLER_NAME,array('Ueditor','Uploadify'))){
        	//return;
        }else{
        	if(session('admin_id') > 0 ){
                $this->check_priv();//检查管理员菜单操作权限
                $this->getMyInfo();
                $this->getMyShopInfo();
        	}else{
        		$this->error('请先登陆',U('Admin/Admin/login'),1);
        	}
        }
        $this->public_assign();
    }
    
    /**
     * 保存公告变量到 smarty中 比如 导航 
     */
    public function public_assign()
    {
       $lnshop_config = array();
       $tp_config = M('config')->select();       
       foreach($tp_config as $k => $v)
       {
          $lnshop_config[$v['inc_type'].'_'.$v['name']] = $v['value'];
       }
       $this->assign('lnshop_config', $lnshop_config);
    }
    
    public function check_priv()
    {
    	$ctl = CONTROLLER_NAME;
    	$act = ACTION_NAME;
		$act_list = session('act_list');
		//无需验证的操作
		$uneed_check = array('login','logout','vertifyHandle','vertify','imageUp','upload','login_task', 'updateStore', 'updateOnSale');
    	if($ctl == 'Index' || $act_list == 'all'){
    		//后台首页控制器无需验证,超级管理员无需验证
    		return true;
    	}elseif(strpos('ajax',$act) || in_array($act,$uneed_check)){
    		//所有ajax请求不需要验证权限
    		return true;
    	}else{
    		//$right = M('system_menu')->where("id in ($act_list)")->cache(true)->getField('right',true);
    		$right = M('system_menu')->where("id in ($act_list)")->getField('right',true);
    		foreach ($right as $val){
    			$role_right .= $val.',';
    		}
            $role_right = explode(',', $role_right);
            // if( $ctl=='Goods' && $act == 'detail' )
            // {
                
            // }
    		//检查是否拥有此操作权限
    		if(!in_array($ctl.'Controller@'.$act, $role_right)){
                // var_dump($ctl, $ctl.'Controller@'.$act, $role_right);die;
    			$this->error('您没有操作权限,请联系超级管理员分配权限',U('Admin/Index/welcome'));
    		}
    	}
    }

    // 获取当前的管理员信息和店铺信息
    public function getMyInfo()
    {
        if( $this->admin_info ) return $this->admin_info;

        if( session('admin_id') )
        {
            $this->admin_info =  M('admin')->where([
                "admin_id" => session('admin_id')
            ])->find();
        }
        return $this->admin_info;
    }

    // 获取当前的店铺信息
    public function getMyShopInfo($onlyId = 0)
    {
        //print_r($this->shop_info);
        //print_r($this->admin_info);
        //die;
        if( $this->admin_info['role_id'] != 8 ) return $this->shop_info;

        if( $this->shop_info ) return $this->shop_info;         

        if( $this->admin_info['user_id'] > 0 )
        {
            $this->shop_info = M('shop')
            ->field('id as shop_id,shop_name,shop_user,shop_tel,shop_status,isvalid, goods_type')
            ->where([
                "user_id" => $this->admin_info['user_id']
            ])->find();

            if( $this->shop_info )
            {
                if( $this->shop_info['shop_status'] != 2 )
                {
                    $this->error("店铺尚未开通");
                }
            }
        }else{
            session('admin_id',0);
            $this->error('无法确认身份信息，请联系系统管理员',U('Admin/Admin/login'));
        }
        //print_r($this->shop_info);die;
        return $this->shop_info;
    }

    //返回信息
    public function returnInfo($status,$msg,$data='')
    {
        header('content-type:application/text;charset:utf-8');
        $json = array(
            'status'=>$status,
            'msg'=>$msg,
            'result'=>$data,
        );
        exit(json_encode($json,JSON_UNESCAPED_UNICODE));
    }
}